<?php

namespace app\client\services;

use app\admin\model\CustomerModel;
use app\admin\model\WithdrawalModel;

class WithdrawalService extends \app\admin\services\BaseService
{
    public static function do($p){
        $rule=[
            'amount'=>'require|gt:0',
            'customer_id'=>'require',
            'realname'=>'require',
            'mobile'=>'require'
        ];
        self::validateRule($rule,$p);
        $customerInfo=CustomerModel::find($p['customer_id']);
        if(WithdrawalModel::count([
            ['customer_id','=',$p['customer_id']],
            ['status','=',0],
        ]))
        {
            api_return(500,'有提现申请正在审核,请通过后再试');
        }
        if(!RedPacketService::getLimit($p['customer_id'])){
            api_return(500,'提现额度不足');
        }
        if($customerInfo->balance<($p['amount']))
        {
            api_return(500,'余额不足');
        }
        $res=WithdrawalModel::editWithReturn($p);
        if($res)
            api_return(200,'申请成功,请等待审核!');
        else
            api_return(500);
    }
    public static function list($p){
        $params=[
            'page'=>($p['pageNo'])??1,
            'pageSize'=>($p['pageSize'])??10,
            'where'=>[
                ['customer_id','=',$p['customer_id']]
            ],
        ];
        $list=WithdrawalModel::getList($params);
        $total=WithdrawalModel::count($params['where']);
        api_return(200,'',['list'=>$list,'total'=>$total]);

    }
}
